//
//  PopAnimationViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/8/13.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "PopAnimationViewController.h"
#import <pop.h>
@interface PopAnimationViewController ()
/** <#something#> */
@property (nonatomic, strong) NSMutableArray  *buttonArray;
/** <#something#> */
@property (nonatomic, strong) NSMutableArray  *viewArray;
@end
static CGFloat const delay = 0.1;
static NSInteger const factor = 5;
static NSInteger const totallCount = 9;
@implementation PopAnimationViewController
/** <#mark#> */
- (NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return  _buttonArray;
}
- (NSMutableArray *)viewArray{
    if(!_viewArray){
        _viewArray = [NSMutableArray array];
    }
    return  _viewArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"pop动画";
    [self setAnimatin];
    
}
- (void)setAnimatin {
    self.view.userInteractionEnabled = NO;
    NSArray *TitleArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    CGFloat kscreenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat kscreenH = [UIScreen mainScreen].bounds.size.height;
    CGFloat w = 80;
    CGFloat h = w;
    NSInteger totallyRow = 3;
    NSInteger totallyCol = 3;
    CGFloat margin = 15;
    CGFloat centerMargin = (kscreenW - totallyCol*w - margin*2)/(totallyCol-1);
    
    for (NSInteger i = 0; i < totallCount; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.buttonArray addObject:button];
        button.backgroundColor     = [UIColor orangeColor];
        button.layer.cornerRadius  = w * 0.5;
        button.layer.masksToBounds = YES;
        [button setTitle:TitleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        NSInteger col = i % totallyCol;
        NSInteger row = i / totallyCol;
        CGFloat x = margin + col * (w+centerMargin);
        CGFloat y = 120+margin + row * (h+centerMargin);
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        //        button.frame = CGRectMake(x, y, w, h);
        //设置动画
        POPSpringAnimation *popAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        CGFloat startY = -kscreenH;
        CGFloat endY   = y;
        popAnim.fromValue = [NSValue valueWithCGRect:CGRectMake(x, startY, w, h)];
        popAnim.toValue   = [NSValue valueWithCGRect:CGRectMake(x, endY, w, h)];
        popAnim.springBounciness = factor;
        popAnim.springSpeed = factor;
        popAnim.beginTime = CACurrentMediaTime() + i * delay;
        [button pop_addAnimation:popAnim forKey:nil];
        
        [self.view addSubview:button];
    }
    ///标题
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"楼上的你还有什么遗言？";
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:16 weight:2.f];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor redColor];
    titleLabel.frame = CGRectMake(100, -kscreenH, 130, 40);
    [self.view addSubview:titleLabel];
    [self.viewArray addObject:titleLabel];
    POPSpringAnimation *titleAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    CGFloat leftX = 100;
    CGFloat leftStartY = -kscreenH;
    CGFloat leftEndY = 50;
    titleAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(leftX, leftStartY)];
    titleAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(leftX, leftEndY)];
    titleAnim.beginTime = CACurrentMediaTime() + totallCount * delay;
    titleAnim.springBounciness = factor;
    titleAnim.springSpeed = factor;
    [titleLabel pop_addAnimation:titleAnim forKey:nil];
    ///图片
    UIImageView *imgView = [[UIImageView alloc]init];
    [self.viewArray addObject:imgView];
    imgView.image = [UIImage imageNamed:@"1.jpg"];
    imgView.frame = CGRectMake(0, -kscreenH, 100, 100);
    [self.view addSubview:imgView];
    
    POPSpringAnimation *imgAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    CGFloat centerX = kscreenW - 100;
    CGFloat startImgY = - kscreenH;
    CGFloat endImgY   = 55;
    imgAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, startImgY)];
    imgAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, endImgY)];
    imgAnim.beginTime = CACurrentMediaTime() + (totallCount+1) * delay;
    imgAnim.springSpeed = factor;
    imgAnim.springBounciness = factor;
    [imgView pop_addAnimation:imgAnim forKey:nil];
    //当最后一个展示完成才可以点击
    [imgAnim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        self.view.userInteractionEnabled = YES;
        NSLog(@"可以点击了");
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (UIButton *btn in self.buttonArray) {
        [btn removeFromSuperview];
    }
    for (UIView *v in self.viewArray) {
        [v removeFromSuperview];
    }
    [self setAnimatin];
}
///按钮点击
- (void)buttonClick:(UIButton *)button{
    NSLog(@"%zd",button.tag);
}
@end
